﻿using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

public interface IThemeGenerator
{
    void Generate();
}

/// <summary>
/// Work flow
///     Step 1: Copy Template Theme folder (images, css) to App_Themes folder///             
///     Step 2: Modify BrandingColor.css 
///     Step 3: Generate images
/// </summary>
public class TaxThemeGenerator : IThemeGenerator
{
    /// <summary>
    /// Path of the theme
    /// </summary>
    private string _DestinationPath;
    /// <summary>
    ///  Path of template theme folder
    /// </summary>
    private string _SourcePath;

    private TaxThemeInformation _ThemeInfo;

    private IImageGenerator _ImageGenerator;

    public TaxThemeGenerator(TaxThemeInformation themeInfo, string sourcePath, string destinationPath)
    {
        this._ThemeInfo = themeInfo;
        this._DestinationPath = destinationPath;
        this._SourcePath = sourcePath;
        this._ImageGenerator = new TaxImageGenerator(this._ThemeInfo, this._DestinationPath);
    }

    public void Generate()
    {
        this.MoveTemplateToAppThemes();
        this.ModifyTemplateCSS();
        this.GenerateImageByColor();
        this.GenerateImageByBitmap();
    }

    protected void MoveTemplateToAppThemes()
    {
        TaxIOExtensions.CopyDirectory(this._SourcePath, this._DestinationPath);
    }
    protected void ModifyTemplateCSS()
    {
        string brandingCSSPath = Path.Combine(this._DestinationPath, "BrandingColor.css");
        StringBuilder cssContent = TaxIOExtensions.ReadTextFile(brandingCSSPath);
        cssContent.Replace("[MASTER_BODER_COLOR]", this._ThemeInfo.BorderColor);
        cssContent.Replace("[BUTTON_SECONDARY_COLOR]", this._ThemeInfo.ButtonSecondaryColor);
        cssContent.Replace("[BUTTON_PRIMARY_COLOR]", this._ThemeInfo.ButtonColor);
        cssContent.Replace("[GRID_LINE_BORDER_COLOR]", this._ThemeInfo.GridLineBorderColor);
        cssContent.Replace("[PRIMARY_COLOR]", this._ThemeInfo.PrimaryColor);
        cssContent.Replace("[GRID_REPORT_TOTAL_BACKGROUND_COLOR]", this._ThemeInfo.ReportTotalBackgroundColor);
        cssContent.Replace("[GRID_ALTER_ROW_BACKGROUND_COLOR]", this._ThemeInfo.GridAlterRowColor);
        cssContent.Replace("[EXPORT_BACKGROUND_COLOR]", this._ThemeInfo.ExportBackgroundColor);
        cssContent.Replace("[DEFAULT_TEXT_COLOR]", this._ThemeInfo.DefaultTextColor);
        cssContent.Replace("[HEADER_TEXT_COLOR]", this._ThemeInfo.HeaderTextColor);
        cssContent.Replace("[MENU_TEXT_COLOR]", this._ThemeInfo.MenuTextColor);
        cssContent.Replace("[PRIMARY_LINK_COLOR]", this._ThemeInfo.PrimaryLinkColor);
        cssContent.Replace("[LOGIN_ADDR_BACKGROUND_COLOR]", this._ThemeInfo.LoginAddrBackgroundColor);
        cssContent.Replace("[LOGIN_ADDR_TEXT_COLOR]", this._ThemeInfo.LoginAddrTextColor);
        cssContent.Replace("[BUTTON_TEXT_COLOR]", this._ThemeInfo.ButtonTextColor);
        cssContent.Replace("[GRID_REPORT_TOTAL_TEXT_COLOR]", this._ThemeInfo.ReportTotalTextColor);
        cssContent.Replace("[GRID_HEADER_TEXT_COLOR]", this._ThemeInfo.GridHeaderTextColor);
        cssContent.Replace("[EXPORT_TEXT_COLOR]", this._ThemeInfo.ExportTextColor);
        cssContent.Replace("[BACKGROUND_COLOR]", this._ThemeInfo.BackgroundColor);
        TaxIOExtensions.WriteTextFile(brandingCSSPath, cssContent);

    }
    protected void GenerateImageByColor()
    {
        this._ImageGenerator.Generate();
    }
    protected void GenerateImageByBitmap()
    {
        string backgroundPath = Path.Combine(this._DestinationPath, "Images\\dot.png");
        string logoPath = Path.Combine(this._DestinationPath, "Images\\aperia-md-color.png");
        if (this._ThemeInfo.BackgroundImage == null)
        {
            Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.BackgroundColor);
            Bitmap pageBackground = new Bitmap(640, 500);
            pageBackground.FillRectangle(0, 0, 640, 500, mainColor);
            GraphicExtension.SaveToPngImage(backgroundPath, pageBackground, 100L);
        }
        else 
        {           
            Bitmap background = new Bitmap(this._ThemeInfo.BackgroundImage);
            GraphicExtension.SaveToPngImage(backgroundPath, background, 100L);
        }

        if (this._ThemeInfo.Logo != null)
        {
            Bitmap logo = new Bitmap(this._ThemeInfo.Logo);
            GraphicExtension.SaveToPngImage(logoPath, logo, 100L);
        }
    }    
}
